use axum::Json;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::Error;

#[derive(Serialize, Deserialize)]
pub struct Resp<T> {
    pub code: i32,
    pub msg: String,
    pub data: T,
}

impl<T: Serialize + DeserializeOwned> Resp<T> {
    pub fn new(code: i32, msg: String, data: T) -> Self {
        Self { code, msg, data }
    }

    pub fn ok(data: T) -> Self {
        Self::new(0, "OK".to_string(), data)
    }
}

impl Resp<()> {
    pub fn ok_empty() -> Self {
        Self::ok(())
    }
    pub fn err(e: Error) -> Self {
        Self::new(-1, e.message, ())
    }
}

pub type JsonResp<T> = Json<Resp<T>>;

#[derive(Serialize, Deserialize)]
pub struct IdResp {
    pub id: String,
}

#[derive(Serialize, Deserialize)]
pub struct AffResp {
    pub rows: u64,
}
